package com.ruoyi.module.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.CullenUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.module.domain.Account;
import com.ruoyi.module.domain.Collect;
import com.ruoyi.module.domain.FundDetails;
import com.ruoyi.module.mapper.AccountMapper;
import com.ruoyi.module.service.IFundDetailsService;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.module.mapper.WithdrawalAmountMapper;
import com.ruoyi.module.domain.WithdrawalAmount;
import com.ruoyi.module.service.IWithdrawalAmountService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 提现管理Service业务层处理
 *
 * @author fubojin
 * @date 2025-01-10
 */
@Service
public class WithdrawalAmountServiceImpl implements IWithdrawalAmountService {
    @Autowired
    private WithdrawalAmountMapper withdrawalAmountMapper;
    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private IFundDetailsService fundDetailsService;

    /**
     * 查询提现管理
     *
     * @param id 提现管理主键
     * @return 提现管理
     */
    @Override
    public WithdrawalAmount selectWithdrawalAmountById(Long id) {
        return withdrawalAmountMapper.selectWithdrawalAmountById(id);
    }

    /**
     * 查询提现管理列表
     *
     * @param withdrawalAmount 提现管理
     * @return 提现管理
     */
    @Override
    public List<WithdrawalAmount> selectWithdrawalAmountList(WithdrawalAmount withdrawalAmount) {
        return withdrawalAmountMapper.selectWithdrawalAmountList(withdrawalAmount);
    }

    /**
     * 新增提现管理
     *
     * @param withdrawalAmount 提现管理
     * @return 结果
     */
    @Override
    public int insertWithdrawalAmount(WithdrawalAmount withdrawalAmount) {
        WithdrawalAmount selectAmount = withdrawalAmountMapper.selectAmountByUserId(withdrawalAmount.getUserId());
        CullenUtils.validateDataThrowException(selectAmount != null, "存在提现待审核数据，不能提现，请联系管理员！");
        withdrawalAmount.setCreateTime(DateUtils.getNowDate());
        withdrawalAmount.setAudit(1);//待审核
        withdrawalAmount.setWithdrawalNumber(OrderUtils.getOrderNo("TX"));//待审核
        return withdrawalAmountMapper.insertWithdrawalAmount(withdrawalAmount);
    }

    /**
     * 修改提现管理
     *
     * @param withdrawalAmount 提现管理
     * @return 结果
     */
    @Override
    public int updateWithdrawalAmount(WithdrawalAmount withdrawalAmount) {
        return withdrawalAmountMapper.updateWithdrawalAmount(withdrawalAmount);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int approve(Long id) {
        WithdrawalAmount withdrawalAmount = withdrawalAmountMapper.selectWithdrawalAmountById(id);
        withdrawalAmount.setAudit(3);
        withdrawalAmount.setAuditTime(DateUtils.getNowDate());
        withdrawalAmountMapper.updateWithdrawalAmount(withdrawalAmount);


        //提现资金明细(订单提成)
        FundDetails fundDetails = new FundDetails();
        SysDept sysDept = deptMapper.selectDeptById(withdrawalAmount.getDeptId());
        fundDetails.setUserId(sysDept.getUserId());//扣除对应的是经销商
        fundDetails.setDeptId(sysDept.getDeptId());
        fundDetails.setDeptName(sysDept.getDeptName());

        fundDetails.setFundType(7);
        fundDetails.setOrderNo(withdrawalAmount.getWithdrawalNumber());
        fundDetails.setVariableAmount(withdrawalAmount.getWithdMount());
        fundDetails.setLoanType(1);
        fundDetailsService.insertFundDetails(fundDetails);

        //个人账户余额处理
        Long userId = withdrawalAmount.getUserId();
        Account account = accountMapper.selectAccountByUserId(userId);//查询是否存在余额
        CullenUtils.validateDataThrowException(account == null, "余额为空，无法提现！");

//        if (account == null) {
//            Account accountAdd = new Account();
//            accountAdd.setUserId(userId);
//            accountAdd.setAccountBalance(withdrawalAmount.getWithdMount());
//            accountAdd.setType(1);
//            accountAdd.setCreateTime(DateUtils.getNowDate());
//            accountMapper.insertAccount(accountAdd);
//        } else {
            CullenUtils.validateDataThrowException(account.getAccountBalance().compareTo(withdrawalAmount.getWithdMount()) < 0, "账户余额已不足，无法审核通过");
            account.setAccountBalance(account.getAccountBalance().subtract(withdrawalAmount.getWithdMount()));
            account.setUpdateTime(DateUtils.getNowDate());
            accountMapper.updateAccount(account);
        return 1;
    }

    @Override
    public int reject(Long id, String auditReason) {
        WithdrawalAmount withdrawalAmount = withdrawalAmountMapper.selectWithdrawalAmountById(id);
        withdrawalAmount.setAudit(4);
        withdrawalAmount.setAuditReason(auditReason);
        withdrawalAmount.setAuditTime(DateUtils.getNowDate());
        withdrawalAmountMapper.updateWithdrawalAmount(withdrawalAmount);
        return 1;
    }

    @Override
    public int cancel(Long id) {
        WithdrawalAmount withdrawalAmount = withdrawalAmountMapper.selectWithdrawalAmountById(id);
        withdrawalAmount.setAudit(2);
        withdrawalAmount.setAuditTime(DateUtils.getNowDate());
        withdrawalAmountMapper.updateWithdrawalAmount(withdrawalAmount);
        return 1;
    }

    /**
     * 批量删除提现管理
     *
     * @param ids 需要删除的提现管理主键
     * @return 结果
     */
    @Override
    public int deleteWithdrawalAmountByIds(Long[] ids) {
        return withdrawalAmountMapper.deleteWithdrawalAmountByIds(ids);
    }

    /**
     * 删除提现管理信息
     *
     * @param id 提现管理主键
     * @return 结果
     */
    @Override
    public int deleteWithdrawalAmountById(Long id) {
        return withdrawalAmountMapper.deleteWithdrawalAmountById(id);
    }
}
